package com.jsoft.jsofted.sys.controller;

import com.jsoft.jsofted.framework.annotation.JwtToken;
import com.jsoft.jsofted.framework.controller.DataController;
import com.jsoft.jsofted.sys.entity.UserEntity;
import com.jsoft.jsofted.framework.service.CocService;
import com.jsoft.jsofted.sys.service.UserService;
import com.jsoft.jsofted.framework.utils.HanyuPinyinUtils;
import com.jsoft.jsofted.framework.utils.ResponseModel;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/jsofted/user")
public class UserController extends DataController<UserEntity> {

    @Autowired
    private UserService userService;

    @Override
    public CocService<UserEntity> cocService() {
        return userService;
    }

    /**
     * 自动生成登录账号
     *
     * @param realName
     * @return
     */
    @JwtToken
    @RequestMapping("/autoUsername/{realName}")
    public ResponseModel autoUsername(@PathVariable String realName) {
        String realNamePY = HanyuPinyinUtils.getFirstLettersLo(realName);
        String username = realNamePY;
        UserEntity user = userService.getByUsername(username);

        int count = 1;
        while (user != null) {
            username = realNamePY + count;
            user = userService.getByUsername(username);
            count++;
        }

        return ResponseModel.makeSuccess(null, username);
    }

}
